	
	/******************************************************************************\
	|    Title:        	 Replication code for Globalization, Government            |
	|                    Popularity, and the Great Skill Divide     			   |
	|    Date:         	 September 2023                                            |
	\******************************************************************************/

*******************************************************************************
**#            	   0 -  Cleaning
*******************************************************************************
	
	* Define path:
		
	cd "../.."
	
	* Define data and other paths:
	global savet "3-replication-package/3-tables"
	global savef "3-replication-package/4-figures"
	
	gl conf "4-confidential-data-not-for-publication"
	gl data "3-replication-package/1-data"
	
	do "3-replication-package/2-code/0-setup"
	
	do "3-replication-package/2-code/0-clean"
	
*******************************************************************************
**#            	   I -  Globals and variable definitions
*******************************************************************************
	gl demographics age age2 male partnered rural

	* Open data
	use "$conf/Gallup_cleaned.dta", replace

	gen sample1 = (!missing(confidence_government))
	gen sample0 = 1
	foreach var of varlist high_export_IV low_export_IV high_import_IV low_import_IV male age age2 partnered tertiary urban rural {
		replace sample1 = 0 if missing(`var')
		replace sample0 = 0 if missing(`var')
	}
	tab sample1
	gen sample2 = sample1
	replace sample2 = 0 if age<25 | age>65

	egen countryyear = group(country_gallup year)
	gen log_gdppc = log(gdppc)

	global demographics "male age age2 partnered rural"	

	label var confidence_government "Confidence in national government"
	label var age "Age"
	label var male "Male"
	label var tertiary "Tertiary education"
	label var partnered "Partnered"
	label var rural "Rural"
	label var log_income_2 "(Log) Household income"
	label var confidence_military "Have confidence in the military"
	label var confidence_elections "Have confidence in honesty of elections"
	label var confidence_courts "Have confidence in the judicial system"
	label var confidence_media "Have confidence in the media"

	* Interactions of less than tertiary:
	foreach var of varlist l_export l_import l_high_export l_high_import{
		gen less_ter_`var' = less_ter*`var'
	}

	* Interactions of tertiary:
	foreach var of varlist l_export l_import l_low_export l_low_import {
		gen tertiary_`var' = tertiary*`var'
	}
	label var less_ter_l_export "No Tertiary* Log Exports"
	label var less_ter_l_import "No Tertiary* Log Imports"
	label var less_ter_l_high_export "No Tertiary* Log High Skill Exports"
	label var less_ter_l_high_import "No Tertiary* Log High Skill Imports"
	label var tertiary_l_export "Tertiary* Log Exports"
	label var tertiary_l_import "Tertiary* Log Imports"
	label var tertiary_l_low_export "Tertiary* Log Low Skill Exports"
	label var tertiary_l_low_import "Tertiary* Log Low Skill Imports"


*******************************************************************************
**# 					II -  Main results
*******************************************************************************
	 
	 
	**# Figure 1: Trends in Confidence in Government
	 
	tab country_gallup year if sample1 == 1

	unique year if sample1==1, by(country_gallup) gen(observed_years)
	tab observed_years
	tab country_gallup if observed_years<7

	bys country_gallup: egen observed_years1 = max(observed_years)

	* Democracies (polity2 >=6)
	preserve

	keep if sample1 == 1 & observed_years1>=7

	keep if polity2 >= 6

	collapse confidence_government [aw=wgt], by(year)
	replace confidence_government = confidence_government*100

	keep if year>=2006 & year<=2018

	twoway conn confidence_government year, lcolor(black) msymbol(Sh) mcolor(black) ytitle("Trends in Confidence in Government") xtitle(Year) xlabel(2006(1)2018) graphregion(color(white)) ylabel(0(25)100)
	gr export "${savef}/Figure1_panela.png", replace
	restore

	* Non democracies (polity2 <= 5)
	preserve

	keep if sample1 == 1 & observed_years1>=7

	keep if polity2 <= 5

	collapse confidence_government [aw=wgt], by(year)
	replace confidence_government = confidence_government*100

	keep if year>=2006 & year<=2018

	twoway conn confidence_government year, lcolor(black) msymbol(Sh) mcolor(black) ytitle("Trends in Confidence in Government") xtitle(Year) xlabel(2006(1)2018) graphregion(color(white)) ylabel(0(25)100)
	gr export "${savef}/Figure1_panelb.png", replace
	restore
	 
	 
	**# Table 1: Sample characteristics - 2005-2018 Gallup World Poll Data
	 
	gen secondary = (education == 2) if !missing(education)
	
	gcollapse (sd) sd_confidence_government = confidence_government sd_l_high_export = l_high_export sd_l_high_import = l_high_import sd_l_low_export = l_low_export sd_l_low_import = l_low_import, by(country_gallup) merge
	
	gl table1 confidence_government l_high_export l_high_import l_low_export l_low_import age male tertiary secondary partnered urban
	 
	eststo clear
	local sample sample1
	estpost tabstat $table1 sd_confidence_government sd_l_high_export sd_l_high_import sd_l_low_export sd_l_low_import if sample1 == 1, stat(mean sd N) col(st)
	esttab . using "${savet}/Table1.rtf", cell("mean(fmt(%9.2f)) sd(fmt(%9.2f)) count(fmt(%9.2g))") label replace nonum
	
	drop sd_*

	**# Figure 2: Exports vs. Predicted Exports and Imports vs Predicted Imports*Bottom

	preserve
	keep if sample1 == 1

	keep l_* high_* low_* country_gallup year
	duplicates drop

	gen l_export_IV = log(high_export_IV+low_export_IV)
	gen l_import_IV = log(high_import_IV+low_import_IV)


	twoway (scatter l_high_export l_high_export_IV) (lfit l_high_export l_high_export_IV), graphregion(color(white)) ytitle("(ln)High-skill intensive exports") xtitle("(ln)Predicted high-skill intensive exports") legend(off) yscale(range(10 30)) ylabel(minmax 10(5)30) xscale(range(10 30)) xlabel(minmax 10(5)30)
	gr export "${savef}/Figure2_panela.png", replace

	twoway (scatter l_high_import l_high_import_IV) (lfit l_high_import l_high_import_IV), graphregion(color(white)) ytitle("(ln)High-skill intensive imports") xtitle("(ln)Predicted high-skill intensive imports") legend(off) yscale(range(10 30)) ylabel(minmax 10(5)30) xscale(range(10 30)) xlabel(minmax 10(5)30)
	gr export "${savef}/Figure2_panelb.png", replace

	twoway (scatter l_low_export l_low_export_IV) (lfit l_low_export l_low_export_IV), graphregion(color(white)) ytitle("(ln)Low-skill intensive exports") xtitle("(ln)Predicted low-skill intensive exports") legend(off) yscale(range(10 30)) ylabel(minmax 10(5)30) xscale(range(10 30)) xlabel(minmax 10(5)30)
	gr export "${savef}/Figure2_panelc.png", replace

	twoway (scatter l_low_import l_low_import_IV) (lfit l_low_import l_low_import_IV), graphregion(color(white)) ytitle("(ln)Low-skill intensive imports") xtitle("(ln)Predicted low-skill intensive imports") legend(off) yscale(range(10 30)) ylabel(minmax 10(5)30) xscale(range(10 30)) xlabel(minmax 10(5)30)
	gr export "${savef}/Figure2_paneld.png", replace

	restore



	**# Table 2: OLS Estimates with Aggregate Exports (Imports) and Tertiary Education Interactions

	local outcome confidence_government

	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
		
			local Xcovariates l_export l_import tertiary
			local Xcovariates1 less_ter_l_export tertiary_l_export less_ter_l_import tertiary_l_import tertiary 
			local Xcovariates2 tertiary_l_export tertiary_l_import tertiary
			local Xordered l_export tertiary_l_export less_ter_l_export l_import tertiary_l_import less_ter_l_import tertiary
			
			eststo clear
			eststo: reghdfe `outcome' `Xcovariates' [aweight = `wgt'] if `sample' == 1, abs(country_gallup year) cl(countryyear)
			qui: estadd local cfe Yes
			qui: estadd local yfe Yes
			qui: estadd local dem No
			qui: estadd local cyfe No
			eststo: reghdfe `outcome' `Xcovariates' $demographics [aweight = `wgt'] if `sample' == 1, abs(country_gallup year) cl(countryyear)
			qui: estadd local cfe Yes
			qui: estadd local yfe Yes
			qui: estadd local dem Yes
			qui: estadd local cyfe No
			eststo: reghdfe `outcome' `Xcovariates1' $demographics [aweight = `wgt'] if `sample' == 1, abs(country_gallup year) cl(countryyear)
			qui: estadd local cfe Yes
			qui: estadd local yfe Yes
			qui: estadd local dem Yes
			qui: estadd local cyfe No
			eststo: reghdfe `outcome' `Xcovariates2' $demographics [aweight = `wgt'] if `sample' == 1, abs(countryyear) cl(countryyear)
			qui: estadd local cfe No
			qui: estadd local yfe No
			qui: estadd local dem Yes
			qui: estadd local cyfe Yes
			
			esttab using "${savet}/Table2.rtf" , keep(`Xordered') order(`Xordered') label mtitle("(1)" "(2)" "(3)" "(4)") nonumber replace r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country*year fixed effects")
			
		}

	}


	**# Table 3: OLS Estimates with High-skill and Low-skill Intensive Exports (Imports) and Education Interactions

	local outcome confidence_government

	foreach wgt of varlist wgt  {
		
		foreach sample of varlist sample1 {
		
			local Xcovariates l_high_export l_high_import l_low_export l_low_import tertiary
			
			local Xcovariates1 less_ter_l_high_export tertiary_l_high_export less_ter_l_high_import tertiary_l_high_import less_ter_l_low_export tertiary_l_low_export less_ter_l_low_import tertiary_l_low_import tertiary
			
			local Xcovariates2 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates2 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			
			local Xordered l_high_export less_ter_l_high_export tertiary_l_high_export l_high_import less_ter_l_high_import tertiary_l_high_import l_low_export less_ter_l_low_export tertiary_l_low_export l_low_import less_ter_l_low_import tertiary_l_low_import tertiary
			
			eststo clear
			eststo: reghdfe `outcome' `Xcovariates' [aweight = `wgt'] if `sample' == 1, abs(country_gallup year) cl(countryyear)
			qui: estadd local cfe Yes
			qui: estadd local yfe Yes
			qui: estadd local dem No
			qui: estadd local cyfe No
			eststo: reghdfe `outcome' `Xcovariates' $demographics  [aweight = `wgt'] if `sample' == 1, abs(country_gallup year) cl(countryyear)
			qui: estadd local cfe Yes
			qui: estadd local yfe Yes
			qui: estadd local dem Yes
			qui: estadd local cyfe No
			eststo: reghdfe `outcome' `Xcovariates1' $demographics  [aweight = `wgt'] if `sample' == 1, abs(country_gallup year) cl(countryyear)
			qui: estadd local cfe Yes
			qui: estadd local yfe Yes
			qui: estadd local dem Yes
			qui: estadd local cyfe No
			eststo: reghdfe `outcome' `Xcovariates2' tertiary $demographics  [aweight = `wgt'] if `sample' == 1, abs(countryyear) cl(countryyear)
			qui: estadd local cfe No
			qui: estadd local yfe No
			qui: estadd local dem Yes
			qui: estadd local cyfe Yes
			eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') tertiary $demographics if `sample' == 1 [aweight = `wgt'], abs(countryyear) r cluster(countryyear) ffirst
			qui: estadd local cfe No
			qui: estadd local yfe No
			qui: estadd local dem Yes
			qui: estadd local cyfe Yes
		
			esttab using "${savet}/Table3.rtf" , keep(`Xordered') order(`Xordered') label mtitle("(1)" "(2)" "(3)" "(4)" "(5) IV") nonumber replace r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country*year fixed effects")
			
		}

	}


	**# Table 4: IV First Stage Results for Confidence in Government Outcome:

	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			local outcome confidence_government
			local Xcovariates1 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates1 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			eststo clear
		
			foreach var of local Xcovariates1 {
				
				qui: eststo: reghdfe `var' `Zcovariates1' tertiary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) cluster(countryyear)
				qui: estadd local cfe No
				qui: estadd local yfe No
				qui: estadd local dem Yes
				qui: estadd local cyfe Yes
				qui: test `Zcovariates1'
				qui: estadd local F_first = round(r(F),0.01)
				
			}
			
			esttab using "${savet}/Table4.rtf", replace keep(`Zcovariates1' tertiary) label mtitle("Tertiary*Log High Skill Exports" "Tertiary*Log High Skill Imports" "Less tertiary*Log Low Skill Exports" "Less tertiary*Log Low Skill Imports") nonumber r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country*year fixed effects" "F_first First Stage F Statistic" "F_KP_T3 Kleibergen-Paap rk Wald F Statistic for joint significance of the instruments for Column 5 of Table 3")
		
	}

	}


	**# Table 6: Mechanisms

	rename emp_2010 emp_status
	gen unemployed=(emp_status==4) if emp_status!=.

	local outcomes unemployed index_ec econ_conditions
	local Xcovariates tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import 
	local Zcovariates tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
	local Xordered tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import tertiary

	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
		eststo clear
		
		foreach outcome in `outcomes' {
		
			eststo: ivreghdfe `outcome' (`Xcovariates' = `Zcovariates') tertiary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local F_KP = round(e(widstat),1)
			
		}
		
		esttab using "${savet}/Table6.rtf" , keep(`Xordered') order(`Xordered') label mtitle("(1) Unemployment" "(2) Economic Confidence Index" "(3) Economics conditions" "(4) Economic conditions getting better") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("F_KP Kleibergen-Paap rk Wald F Statistic") 
		
		}
		
	}


*******************************************************************************
**#   					  III -  Appendix
*******************************************************************************


	**# Appendix Figure 2: Yearly Elasticity of Trade Costs (CIF-FOB) for High Skill Intensive Trade

	foreach trade of newlist sea air {
		
		foreach type of newlist high low {  
			
			matrix `trade'_`type' = J(19,4,.)
			matrix colnames `trade'_`type' = coef ll95 ul95 se
		
		}

	}

	* Sea high:
	matrix sea_high[1.19,1] = 0.016 \ 0.03 \ 0.044 \ 0.028 \ 0.019 \ 0.02 \ 0.023 \ -0.003 \ -0.003 \ -0.009 \ 0.02 \ 0.019 \ 0.029 \ 0.02 \ 0.012 \ 0.013 \ 0.017 \ 0.003 \ 0.015

	matrix air_high[1.19,1] = 0.307 \ 0.292 \ 0.276 \ 0.293 \ 0.3 \ 0.291 \ 0.306 \ 0.345 \ 0.341 \ 0.358 \ 0.324 \ 0.336 \ 0.326 \ 0.349 \ 0.376 \ 0.358 \ 0.354 \ 0.398 \ 0.402

	matrix sea_low[1.19,1] = 0.29 \ 0.328 \ 0.304 \ 0.307 \ 0.306 \ 0.319 \ 0.317 \ 0.309 \ 0.322 \ 0.313 \ 0.319 \ 0.308 \ 0.324 \ 0.337 \ 0.335 \ 0.346 \ 0.369 \ 0.446 \ 0.401

	matrix air_low[1.19,1] = 0.027 \ 0.005 \ 0.019 \ 0.021 \ 0.02 \ 0.007 \ 0.016 \ 0.024 \ 0.02 \ 0.025 \ 0.028 \ 0.036 \ 0.03 \ 0.023 \ 0.041 \ 0.032 \ 0.008 \ 0.019 \ 0.023


	matrix sea_high[1.19,4] = 0.016 \ 0.015 \ 0.017 \ 0.014 \ 0.013 \ 0.012 \ 0.012 \ 0.013 \ 0.014 \ 0.013 \ 0.013 \ 0.014 \ 0.014 \ 0.014 \ 0.013 \ 0.014 \ 0.014 \ 0.017 \ 0.015

	matrix air_high[1.19,4] = 0.021 \ 0.02 \ 0.022 \ 0.019 \ 0.018 \ 0.017 \ 0.015 \ 0.017 \ 0.018 \ 0.018 \ 0.017 \ 0.017 \ 0.018 \ 0.018 \ 0.016 \ 0.018 \ 0.017 \ 0.022 \ 0.028

	matrix sea_low[1.19,4] = 0.017 \ 0.017 \ 0.016 \ 0.017 \ 0.016 \ 0.015 \ 0.015 \ 0.015 \ 0.015 \ 0.016 \ 0.016 \ 0.017 \ 0.016 \ 0.015 \ 0.017 \ 0.015 \ 0.015 \ 0.023 \ 0.018

	matrix air_low[1.19,4] = 0.012 \ 0.013 \ 0.012 \ 0.013 \ 0.012 \ 0.012 \ 0.011 \ 0.012 \ 0.011 \ 0.012 \ 0.012 \ 0.012 \ 0.012 \ 0.011 \ 0.012 \ 0.012 \ 0.011 \ 0.018 \ 0.021


	foreach trade of newlist sea air {
		
		foreach type of newlist high low {  
			
			foreach num of numlist 1/19 {
				
				matrix `trade'_`type'[`num',2] = `trade'_`type'[`num',1] - 1.95*`trade'_`type'[`num',4]
				matrix `trade'_`type'[`num',3] = `trade'_`type'[`num',1] + 1.95*`trade'_`type'[`num',4]
			
			}
			
			matrix `trade'_`type' = `trade'_`type''
			matrix colnames `trade'_`type' = 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
		
		}

	}

	local dim (`= rowsof(sea_high)',`=colsof(sea_high)')
	di "`dim'"

	coefplot matrix(sea_high) matrix(air_high), ci((2 3)) vertical yline(0) graphregion(color(white)) ciopts(recast(rcap)) citop msymbol(d) ylabel(0(0.05)0.5) legend(order(1 "Sea" 2 "95% CI" 3 "Air" 4 "95% CI")) xlabel(,ang(45)) offset(0)
	gr export "${savef}/AppendixFigure2.png", replace


	**# Appendix Figure 3: Yearly Elasticity of Trade Costs (CIF-FOB) for Medium + Low Skill Intensive Trade

	coefplot matrix(sea_low) matrix(air_low), ci((2 3)) vertical yline(0) graphregion(color(white)) ciopts(recast(rcap)) citop msymbol(d) ylabel(0(0.05)0.5) legend(order(1 "Sea" 2 "95% CI" 3 "Air" 4 "95% CI")) xlabel(,ang(45)) offset(0)
	gr export "${savef}/AppendixFigure3.png", replace



	**# Appendix Table 1: Expanded Set of Coefficients, Fully Saturated Model (i.e. Column 4 of Table 3)

	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			local outcome confidence_government
			local Xcovariates1 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates1 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			
			eststo clear
			qui: eststo: reghdfe `outcome' `Xcovariates1' tertiary $demographics [aweight = `wgt'] if `sample' == 1, abs(countryyear) cl(countryyear)
			qui estadd local cfe No
			qui estadd local yfe No
			qui estadd local cyfe Yes
			qui: eststo: ivreghdfe `outcome' (`Xcovariates1' = `Zcovariates1') tertiary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui qui estadd local cfe No
			qui estadd local yfe No
			qui estadd local cyfe Yes
			qui estadd local F_KP = round(e(widstat),1)
			
			di "`wgt', `sample'"
			
			esttab using "${savet}/AppendixTable1.rtf" , keep(`Xcovariates1' tertiary $demographics) label mtitle("(1)" "(2)") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "cyfe Country-year fixed effects" "F_KP Kleibergen-Paap rk Wald F Statistic")
		
		}

	}


	**# Appendix Table 2: Robustness for leader approval

	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			local outcome approvaloftheleader
			local Xcovariates1 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates1 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			
			eststo clear
			eststo: reghdfe `outcome' `Xcovariates1' tertiary $demographics [aweight = `wgt'] if `sample' == 1, abs(countryyear) cl(countryyear)
			qui estadd local cfe No
			qui estadd local yfe No
			qui estadd local cyfe Yes
			eststo: ivreghdfe `outcome' (`Xcovariates1' = `Zcovariates1') tertiary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui qui estadd local cfe No
			qui estadd local yfe No
			qui estadd local cyfe Yes
			qui estadd local F_KP = round(e(widstat),1)
			
			di "`wgt', `sample'"
			
			esttab using "${savet}/AppendixTable2.rtf" , keep(`Xcovariates1' tertiary) label mtitle("(1)" "(2)") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "cyfe Country-year fixed effects" "F_KP Kleibergen-Paap rk Wald F Statistic")
		
		}

	}


	**# Appendix Table 3: IV Estimates with Alternative Skilled Definition (Secondary education or above)

	gen abovesec = (education == 2 | education == 3) if !missing(education)
	gen primary = (education == 1) if !missing(education)

	foreach var of varlist l_high_* {
		gen abovesec_`var' = abovesec*`var'  if !missing(abovesec) & !missing(`var')
	}
	label var abovesec_l_high_export "Secondary or Tertiary*Log High Skill Exports"
	label var abovesec_l_high_import "Secondary or Tertiary*Log High Skill Imports"

	foreach var of varlist l_low_* {
		gen primary_`var' = primary*`var'  if !missing(primary) & !missing(`var')
	}
	label var primary_l_low_export "Elementary*Log Low Skill Exports"
	label var primary_l_low_import "Elementary*Log Low Skill Imports"

	label var abovesec "Secondary Education or Above"


	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			local outcome confidence_government
			local Xcovariates abovesec_l_high_export abovesec_l_high_import primary_l_low_export primary_l_low_import
			local Zcovariates abovesec_l_high_export_IV abovesec_l_high_import_IV primary_l_low_export_IV primary_l_low_import_IV
			
			eststo clear
			eststo: reghdfe `outcome' `Xcovariates' abovesec $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) cluster(countryyear)
			eststo: ivreghdfe `outcome' (`Xcovariates' = `Zcovariates') abovesec $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local F_KP = round(e(widstat),1)
			
			di "`wgt', `sample'"
			
			esttab using "${savet}/AppendixTable3.rtf" , keep(`Xcovariates' abovesec) label mtitle("OLS" "IV") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("F_KP Kleibergen-Paap rk Wald F Statistic")
		
		}

	}



	**# Appendix Table 4: IV Estimates with High-skill Trade and Secondary and Tertiary Education Interactions

	foreach var of varlist l_low_export* l_low_import* l_high_export* l_high_import* {
		gen secondary_`var' = secondary*`var' if sample1 == 1 & !missing(secondary)
		}
	foreach var of varlist l_low_export_IV l_low_import_IV {
		gen tertiary_`var' = tertiary*`var' if sample1 == 1 & !missing(tertiary)
	}

		
	label var secondary "Secondary Education"
	label var secondary_l_high_export "Secondary * Log High Exports"
	label var secondary_l_high_import "Secondary * Log High Imports"
	label var secondary_l_low_export "Secondary * Log Low Exports"
	label var secondary_l_low_import "Secondary * Log Low Imports"


	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			local outcome confidence_government
			local Xcovariates secondary_l_high_export tertiary_l_high_export secondary_l_high_import tertiary_l_high_import secondary_l_low_export tertiary_l_low_export secondary_l_low_import tertiary_l_low_import
			local Zcovariates secondary_l_high_export_IV tertiary_l_high_export_IV secondary_l_high_import_IV tertiary_l_high_import_IV secondary_l_low_export_IV tertiary_l_low_export_IV secondary_l_low_import_IV tertiary_l_low_import_IV
			
			eststo clear
			eststo: reghdfe `outcome' `Xcovariates' tertiary secondary $demographics [aweight = `wgt'] if `sample' == 1, abs(countryyear) cluster(countryyear)
			eststo: ivreghdfe `outcome' (`Xcovariates' = `Zcovariates') tertiary secondary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local F_KP = round(e(widstat),1)
			
			esttab using "${savet}/AppendixTable4.rtf" , keep(`Xcovariates' tertiary secondary) label mtitle("(1) OLS" "(2) IV") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars( "F_KP Kleibergen-Paap rk Wald F Statistic")	
			
		}

	}


	**# Appendix Table 5: Robustness to Excluding Sub-Periods, Confidence in Government.

	local sample sample1
	gen subsample1 = (!inlist(year,2005,2006,2007)) if `sample' == 1
	gen subsample2 = (!inlist(year,2008,2009,2010)) if `sample' == 1
	gen subsample3 = (!inlist(year,2009,2010,2011)) if `sample' == 1
	gen subsample4 = (!inlist(year,2012,2013,2014,2015)) if `sample' == 1
	gen subsample5 = (!inlist(year,2016,2017,2018)) if `sample' == 1
	global subsamples "subsample1 subsample2 subsample3 subsample4 subsample5"

	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			local outcome confidence_government
			local Xcovariates l_high_export tertiary_l_high_export l_high_import tertiary_l_high_import l_low_export less_ter_l_low_export l_low_import less_ter_l_low_import
			local Zcovariates l_high_export_IV tertiary_l_high_export_IV l_high_import_IV tertiary_l_high_import_IV l_low_export_IV less_ter_l_low_export_IV l_low_import_IV less_ter_l_low_import_IV
			local Xcovariates1 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates1 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			
			eststo clear
			foreach sample of global subsamples {
				
				eststo: ivreghdfe `outcome' (`Xcovariates1' = `Zcovariates1') tertiary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) r cluster(countryyear) ffirst
				qui estadd local cfe No
				qui estadd local yfe No
				qui estadd local dem Yes
				qui estadd local cyfe Yes
				qui estadd local F_KP = round(e(widstat),1)
				
			}
			
			esttab using "${savet}/AppendixTable5.rtf" , keep(`Xcovariates1' tertiary) label mtitle("(1)" "(2)" "(3)" "(4)" "(5)") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country-year fixed effects" "F_CD Cragg-Donald Wald F Statistic" "F_KP Kleibergen-Paap rk Wald F Statistic")
		
		}

	}	
	drop subsample*


	**# Appendix Table 6: Sub-sample Analysis by Gender and Age

		sum age if (age>=18 & age<=64) & sample1==1, d
		* Subsample1: Male
		gen subsample1 = (male == 1) if sample1 == 1
		* Subsample2: female
		gen subsample2 = (male == 0) if sample1 == 1
		* Subsample3: 18-37
		gen subsample3 = (age>=18 & age<=37) if sample1 == 1
		* Subsample4: 38-64
		gen subsample4 = (age>37 & age <=64) if sample1 == 1
		gl subsamples subsample1 subsample2 subsample3 subsample4
		

	foreach wgt of varlist wgt{
		
		local outcome confidence_government
		local Xcovariates1 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
		local Zcovariates1 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
		local subsamples subsample1 subsample2 subsample3 subsample4
		eststo clear
		
		foreach subsample of local subsamples { 
			
		qui: eststo: ivreghdfe `outcome' (`Xcovariates1' = `Zcovariates1') tertiary $demographics [aweight=`wgt'] if `subsample' == 1, abs(countryyear) r cluster(countryyear) ffirst
		estadd local F_KP = round(e(widstat),1)
		
		}
		
		di "`wgt'"
		
		esttab using "${savet}/AppendixTable6.rtf" , keep(`Xcovariates1' tertiary) label mtitle("Male" "Female" "Age 18-37" "Age 38-64") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("F_KP Kleibergen-Paap rk Wald F Statistic") 

	}

	drop subsample*


	**# Appendix Table 7: IV Estimates - Sub-sample Analysis by Urbanization and Household Income

	bysort country_gallup year: egen NN = count(sample1) if sample1 == 1 & !missing(gdppc)
	gen nn = 1/NN

	gen median_gdp = .
	tab year 
	foreach yr of numlist 2005/2018 {
		
		sum gdppc [aw=nn] if sample1 == 1 & !missing(gdppc) & year== `yr', d
		replace median_gdp = r(p50) if year== `yr'
		
	}

	bys countryyear: egen median_income = median(income_4)
		* Subsample1: Urban Countries
	gen subsample1 = 1 if sample1 == 1 & urban == 1
		* Subsample2: Rural Countries
	gen subsample2 = 1 if sample1 == 1 & rural == 1
		* Subsample3: Countries below median income
	gen subsample3 = 1 if sample1 == 1 & income_4 < median_income
		* Subsample4: Countries above median income
	gen subsample4 = 1 if sample1 == 1 & income_4 > median_income


	gl subsamples subsample1 subsample2 subsample3 subsample4

	foreach wgt of varlist wgt {
		
		local outcome confidence_government
		local Xcovariates1 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
		local Zcovariates1 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
		local subsamples subsample1 subsample2 subsample3 subsample4
		eststo clear
		
		foreach subsample of local subsamples { 
			
			qui: eststo: ivreghdfe `outcome' (`Xcovariates1' = `Zcovariates1') tertiary $demographics [aweight=`wgt'] if `subsample' == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local F_KP = round(e(widstat),1)
		
		}
		
		esttab using "${savet}/AppendixTable7.rtf" , keep(`Xcovariates1' tertiary) label mtitle("Urban" "Rural" "Below median income HH" "Above median income HH") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("F_KP Kleibergen-Paap rk Wald F Statistic")

	}
	drop subsample*


	**# Appendix Table 8: IV Estimates – Heterogeneity by Country-level Characteristics
		
	bys stata_id : egen average_export1=mean(l_export) if year!=. & sample1 == 1
	bys stata_id : egen average_import1=mean(l_import) if year!=. & sample1 == 1
	
	gen difference1=average_export1-average_import1 if sample1 == 1
	
	bys stata_id : gen pl_exports=1==difference1>0 & difference1!=. if sample1 == 1
	bys stata_id : gen mi_exports=1==difference1<0 & difference1!=. if sample1 == 1
	
	gen net_exporter = .
	replace net_exporter = 1 if pl_export == 1
	replace net_exporter = 0 if mi_export == 1
	
	gen lowincome1 = .
	replace lowincome1 = 1 if (lowincome ==1) & sample1 == 1
	replace lowincome1 = 0 if (lowermiddleincome == 1 | uppermiddleincome == 1 | highincome == 1) & sample1 == 1
	
	label var lowincome1 "Low-income country"
	
	gen nondemocracy2 = .
	replace nondemocracy2 = 1 if polity2 < 6 & sample1 == 1
	replace nondemocracy2 = 0 if polity2 >= 6 & sample1 == 1
	label var nondemocracy2 "Non-democracy (<6)"

		

	eststo clear
	local outcomes confidence_government
	local interactions net_exporter lowincome1 nondemocracy2

	foreach outcome in `outcomes'{
		
		eststo clear
		
		foreach interaction in `interactions'{
			
			local Xcovariates2 c.tertiary_l_high_export#i.`interaction' c.tertiary_l_high_import#i.`interaction' c.less_ter_l_low_export#i.`interaction'  c.less_ter_l_low_import#i.`interaction' tertiary_l_high_export tertiary_l_high_import  less_ter_l_low_export  less_ter_l_low_import
			
			local Zcovariates2 c.tertiary_l_high_export_IV#i.`interaction'  c.tertiary_l_high_import_IV#i.`interaction' c.less_ter_l_low_export_IV#i.`interaction'  c.less_ter_l_low_import_IV#i.`interaction' tertiary_l_high_export_IV tertiary_l_high_import_IV  less_ter_l_low_export_IV  less_ter_l_low_import_IV
				
			local Xordered c.tertiary_l_high_export#i.`interaction' c.tertiary_l_high_import#i.`interaction'  c.less_ter_l_low_export#i.`interaction'  c.less_ter_l_low_import#i.`interaction'   tertiary
				
			
				
				eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') i.`interaction' tertiary $demographics [aweight = wgt] if sample1 == 1, abs(countryyear) r cluster(countryyear) ffirst
				qui estadd local cfe No
				qui estadd local yfe No
				qui estadd local dem Yes
				qui estadd local cyfe Yes
				qui estadd scalar F_KP = round(e(widstat),1)
				
		}
		
		esttab using "${savet}/AppendixTable8.rtf" , label mtitle("Net high skill exporter" "Low income country" "Non-democracy (average Polity2 < 6)") nonumber replace b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country-year fixed effects" "F_KP Kleibergen-Paap rk Wald F Statistic")

	}



	**# Appendix Table 9: OLS Estimates – Non-linearities 

	tab year

	foreach skill of newlist high low { 
		
		foreach trade of newlist import export {
			
			gen lagged_l_`skill'_`trade' = .
		
			foreach yr of numlist 2006/2018{
				
				bys stata_id: gen lagged = l_`skill'_`trade' if (year== `yr' - 1) & sample1 == 1
				bys stata_id: egen lagged1 = min(lagged) if sample1 == 1
				replace lagged_l_`skill'_`trade' = lagged1 if sample1==1 & year == `yr'
				drop lagged lagged1
			
			} 
			
			gen diff_l_`skill'_`trade' = l_`skill'_`trade'-lagged_l_`skill'_`trade'
		
		}
		
	}

	gen samples0 = (sample1==1 & !missing(lagged_l_high_import))
	bys country_gallup year: egen nn1 = count(samples0) if !missing(lagged_l_high_import)
	gen weights1 = 1/nn1
		
	foreach skill of newlist high low { 
		
		foreach trade of newlist import export {
		
			qui:sum diff_l_`skill'_`trade' [aw=weights1], d
			gen top25_`skill'_`trade' = (diff_l_`skill'_`trade' >= r(p75)) if samples0 == 1
			gen top50_`skill'_`trade' = (diff_l_`skill'_`trade' >= r(p50) & diff_l_`skill'_`trade' < r(p75) ) if samples0 == 1
			gen bottom50_`skill'_`trade' = (diff_l_`skill'_`trade' < r(p50) & diff_l_`skill'_`trade' >= r(p25) )  if samples0 == 1
			gen bottom25_`skill'_`trade' = (diff_l_`skill'_`trade' < r(p25))  if samples0 == 1
		
		}

	}
	sum top* bottom*


	foreach num of numlist 25 50 {
		
		gen tertiary_top`num'_high_export = tertiary*top`num'_high_export*l_high_export
		gen tertiary_top`num'_high_import = tertiary*top`num'_high_import*l_high_import
		gen less_ter_top`num'_low_export = less_ter*top`num'_low_export*l_low_export
		gen less_ter_top`num'_low_import = less_ter*top`num'_low_import*l_low_import
		
		label var tertiary_top`num'_high_export "Tertiary*Log High Skill Exports*Top `num'%"
		label var tertiary_top`num'_high_import "Tertiary*Log High Skill Imports*Top `num'%"
		label var less_ter_top`num'_low_export "No Tertiary*Log Low Skill Exports*Top `num'%"
		label var less_ter_top`num'_low_import "No Tertiary*Log Low Skill Imports*Top `num'%"
		
		gen tertiary_bottom`num'_high_export = tertiary*bottom`num'_high_export*l_high_export
		gen tertiary_bottom`num'_high_import = tertiary*bottom`num'_high_import*l_high_import
		gen less_ter_bottom`num'_low_export = less_ter*bottom`num'_low_export*l_high_export
		gen less_ter_bottom`num'_low_import = less_ter*bottom`num'_low_import*l_high_import
		
		label var tertiary_bottom`num'_high_export "Tertiary*Log High Skill Exports*Bottom `num'%"
		label var tertiary_bottom`num'_high_import "Tertiary*Log High Skill Imports*Bottom `num'%"
		label var less_ter_bottom`num'_low_export "No Tertiary*Log Low Skill Exports*Bottom `num'%"
		label var less_ter_bottom`num'_low_import "No Tertiary*Log Low Skill Imports*Bottom `num'%"	
		
	}


	foreach wgt of varlist wgt {
		
		foreach sample of varlist sample1 {
			
			eststo clear
			local outcome confidence_government
			
			local Xcovariates tertiary_bottom25_high_export tertiary_bottom50_high_export tertiary_top50_high_export tertiary_top25_high_export tertiary_bottom25_high_import tertiary_bottom50_high_import tertiary_top50_high_import tertiary_top25_high_import less_ter_bottom25_low_export less_ter_bottom50_low_export less_ter_top50_low_export less_ter_top25_low_export less_ter_bottom25_low_import less_ter_bottom50_low_import less_ter_top50_low_import less_ter_top25_low_import
			
			
			
			eststo: reghdfe `outcome' `Xcovariates' tertiary $demographics [aweight=`wgt'] if `sample' == 1, abs(countryyear) cluster(countryyear)
			
			di "`wgt', `sample'"
			
			esttab using "${savet}/AppendixTable9.rtf" , keep(`Xcovariates' tertiary) label mtitle("(1)") nonumber replace b(4) se(4)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted
		
		}

	}


	**# Appendix Table 10: Robustness to Using Balanced Sample of Countries

	* Subsample with countries observed for over 6 years
	gen subsample1 = (observed_years1 >= 6) if sample1 == 1
		* Subsample with countries observed for over 7 years
	gen subsample2 = (observed_years1 >= 7) if sample1 == 1
		* Subsample with countries observed for over 8 years
	gen subsample3 = (observed_years1 >= 8) if sample1 == 1
		* Subsample with countries observed for over 9 years
	gen subsample4 = (observed_years1 >= 9) if sample1 == 1

	* Count of countries by observed years
	unique(country_g) if subsample1 == 1
	unique(country_g) if subsample2 == 1
	unique(country_g) if subsample3 == 1
	unique(country_g) if subsample4 == 1


	local outcome confidence_government
	local subsamples subsample1 subsample2 subsample3 subsample4

	foreach wgt of varlist wgt  {
		
		eststo clear
		
		foreach subsample of varlist `subsamples' {
			
			
			local Xcovariates2 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates2 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			
			local Xordered tertiary_l_high_export tertiary_l_high_import  less_ter_l_low_export less_ter_l_low_import tertiary
			
			eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') tertiary $demographics [aweight = `wgt']  if `subsample' == 1 & sample1 == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local cfe No
			qui estadd local yfe No
			qui estadd local dem Yes
			qui estadd local cyfe Yes
			qui estadd scalar F_KP = round(e(widstat),0.01)
			
		}
		
		esttab using "${savet}/AppendixTable10.rtf" , keep(`Xordered') order(`Xordered') label mtitle("(1) Observed years >=6" "(2) >=7" "(3) >= 8" "(4) >= 9") nonumber replace r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country*year fixed effects" "F_KP Kleibergen-Paap rk Wald F Statistic") 
		
	}

	drop subsample*


	**# Appendix Table 11: Robustness to Dropping One Global Region at a Time

	decode reg2_g, gen(region)
	decode reg_g, gen(region2)

	gen subsample1 = (region2 ~= "Northern America" & country_gallup ~= "Mexico") if sample1 == 1
	gen subsample2 = (region2 ~= "Latin America and the Caribbean" | country_gallup == "Mexico") if sample1 == 1
	gen subsample3 = (region ~= "Asia") if sample1 == 1
	gen subsample4 = (region ~= "Europe") if sample1 == 1
	gen subsample5 = (region ~= "Former Soviet Union") if sample1 == 1
	gen subsample6 = (region ~= "Middle East and North Africa") if sample1 == 1
	gen subsample7 = (region ~= "Sub-Saharan Africa") if sample1 == 1

	local subsamples subsample1 subsample2 subsample3 subsample4 subsample5 subsample6 subsample7

	foreach wgt in wgt{
		
		eststo clear
		
		local Xcovariates2 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
		local Zcovariates2 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
		
		local Xordered tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import tertiary
			
		foreach subsample in `subsamples'{
			
			eststo: ivreghdfe confidence_government (`Xcovariates2' = `Zcovariates2') tertiary $demographics [aweight = `wgt'] if `subsample' == 1 & sample1 == 1, abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local cfe No
			qui estadd local yfe No
			qui estadd local dem Yes
			qui estadd local cyfe Yes
			qui estadd scalar F_KP = round(e(widstat),1)
			
		
		}
		
		esttab using "${savet}/AppendixTable11.rtf" , keep(`Xordered') order(`Xordered') label mtitle("Excl. USA/Canada/Mexico" "Latin America and the Caribbean (-Mexico)" "Asia" "Europe" "Former Soviet Union" "Middle East and North Africa" "Sub-Saharan Africa") nonumber replace r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country-year fixed effects" "F_KP First-stage F Statistic (KP)")
		
	}

	drop subsample*
	drop region region2


	**# Appendix Table 12: Robustness to controlling for country-level unemployment rate and gdp growth

	gcollapse (mean) unemployment_rate = unemployed, by(country_g year) merge
	
	replace unemployment_rate = log(unemployment_rate * 100)
	lab var unemployment_rate "(log) Unemployment rate"
	
	local outcome confidence_government
	eststo clear
	
	foreach wgt of varlist wgt  {
		
		foreach sample of varlist sample1 {
			
			local Xcovariates2 tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import
			local Zcovariates2 tertiary_l_high_export_IV tertiary_l_high_import_IV less_ter_l_low_export_IV less_ter_l_low_import_IV
			
			local Xordered tertiary_l_high_export tertiary_l_high_import less_ter_l_low_export less_ter_l_low_import tertiary
			
			eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') tertiary $demographics log_gdppc unemployment_rate if `sample' == 1 [aweight = `wgt'], abs(country_gallup year) r cluster(countryyear) ffirst
			qui estadd local cfe Yes
			qui estadd local yfe Yes
			qui estadd local dem Yes
			qui estadd local cyfe No
			qui estadd scalar F_KP = round(e(widstat),1)
			
			eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') tertiary $demographics log_gdppc if `sample' == 1 [aweight = `wgt'], abs(country_gallup year) r cluster(countryyear) ffirst
			qui estadd local cfe Yes
			qui estadd local yfe Yes
			qui estadd local dem Yes
			qui estadd local cyfe No
			qui estadd scalar F_KP = round(e(widstat),1)
			
			eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') tertiary $demographics unemployment_rate if `sample' == 1 [aweight = `wgt'], abs(country_gallup year) r cluster(countryyear) ffirst
			qui estadd local cfe Yes
			qui estadd local yfe Yes
			qui estadd local dem Yes
			qui estadd local cyfe No
			qui estadd scalar F_KP = round(e(widstat),1)
			
			esttab using "${savet}/AppendixTable12.rtf" , keep(`Xordered' log_gdppc unemployment_rate) order(`Xordered' log_gdppc unemployment_rate) label mtitle("(1) IV" "(2) IV" "(3) IV") nonumber replace r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("cfe Country fixed effects" "yfe Year fixed effects" "dem Demographic characteristics" "cyfe Country*year fixed effects" "F_KP First-stage F Statistic (KP)")
			
		}
	
	}



*******************************************************************************
**# 						IV -  Using ESS 
*******************************************************************************


	use "$data/ESS_cleaned.dta", clear

		** Generate trade variables and interactions:
	gen high_export =  highskilled_exports + mediumskilled_exports
	gen low_export = lowskilled_exports + labourintensive_exports

	foreach var of varlist high_export low_export {
		gen l_`var' = log(`var')
	}
	label var l_high_export "Log High Skill Exports"	
	label var l_low_export "Log Low Skill Exports"	
	label var Dif_l_high_export_1 "Δ Log Predicted High Skill Exports"	
	label var Dif_l_low_export_1 "Δ Log Predicted Low Skill Exports"	
		
	gen high_import =  highskilled_imports + mediumskilled_imports
	gen low_import = lowskilled_imports + labourintensive_imports

	foreach var of varlist high_import low_import {
		gen l_`var' = log(`var')
	}
	label var l_high_import "Log High Skill Imports"	
	label var l_low_import "Log Low Skill Imports"	
	label var Dif_l_high_import_1 "Δ Log High Skill Imports"	
	label var Dif_l_low_import_1 "Δ Log Low Skill Imports"	
	
	gen highsk = (highskilled == 1)
	replace highsk = . if missing(highskilled)
	label var highsk "High Skill Occupation"
	
	gen lowsk = (lowskilled == 1 | midskilled == 1)
	replace lowsk = . if missing(highsk)
	label var lowsk "Low and Medium Skill Occupation"
	
	foreach var of varlist l_high_export l_high_import {
		gen highsk_`var' = highsk*`var'
	}
	*label var highsk_l_low_export "High Skill Occ.* Log Low Skill Exports"
	*label var highsk_l_low_import "High Skill Occ.* Log Low Skill Imports"
	label var highsk_l_high_export "High Skill Occ.* Log High Skill Exports"
	label var highsk_l_high_import "High Skill Occ.* Log High Skill Imports"
	*label var highsk_Dif_l_high_export_1 "High Skill * Δ Log High Skill Exports"
	*label var highsk_Dif_l_high_import_1 "High Skill * Δ Log High Skill Imports"
	
	foreach var of varlist  l_low_export l_low_import {
		gen lowsk_`var' = lowsk*`var'
	}
	*label var lowsk_l_high_export "Low and Medium Skill Occ.* Log High Skill Exports"
	*label var lowsk_l_high_import "Low and Medium Skill Occ.* Log High Skill Imports"
	label var lowsk_l_low_export "Low and Medium Skill Occ.* Log Low Skill Exports"
	label var lowsk_l_low_import "Low and Medium Skill Occ.* Log Low Skill Imports"
	*label var lowsk_Dif_l_low_export_1 "Low and Medium Skill * Δ Log Low Skill Exports"
	*label var lowsk_Dif_l_low_import_1 "Low and Medium Skill * Δ Log Low Skill Imports"
	
	** Predicted trade variables:
	gen high_export_IV =  IV_export_1_pair_highskilled + IV_export_1_pair_mediumskilled
	gen low_export_IV = IV_export_1_pair_lowskilled + IV_export_1_pair_labourintensive

	foreach var of varlist high_export_IV low_export_IV {
		gen l_`var' = log(`var')
	}
	label var l_high_export_IV "Log Predicted High Skill Exports"	
	label var l_low_export_IV "Log Predicted Low Skill Exports"	
	label var Dif_l_high_export_IV_1 "Δ Log Predicted High Skill Exports"	
	label var Dif_l_low_export_IV_1 "Δ Log Predicted Low Skill Exports"	
		
	gen high_import_IV =  IV_import_1_pair_highskilled + IV_import_1_pair_mediumskilled
	gen low_import_IV = IV_import_1_pair_lowskilled + IV_import_1_pair_labourintensive

	foreach var of varlist high_import_IV low_import_IV {
		gen l_`var' = log(`var')
	}
	label var l_high_import_IV "Log Predicted High Skill Imports"	
	label var l_low_import_IV "Log Predicted Low Skill Imports"	
	label var Dif_l_high_import_IV_1 "Δ Log Predicted High Skill Imports"	
	label var Dif_l_low_import_IV_1 "Δ Log Predicted Low Skill Imports"	
	
	foreach var of varlist l_high_export_IV l_high_import_IV {
		gen highsk_`var' = highsk*`var'
	}
	label var highsk_l_high_export_IV "High Skill Occ.* Log Predicted High Skill Exports"
	label var highsk_l_high_import_IV "High Skill Occ.* Log Predicted High Skill Imports"
	*label var highsk_Dif_l_high_export_IV_1 "High Skill Occ.* Δ Log Predicted High Skill Exports"
	*label var highsk_Dif_l_high_import_IV_1 "High Skill Occ.* Δ Log Predicted High Skill Imports"

	foreach var of varlist l_low_export_IV l_low_import_IV  {
		gen lowsk_`var' = lowsk*`var'
	}
	label var lowsk_l_low_export_IV "Low and Medium Skill Occ.* Log Predicted Low Skill Exports"
	label var lowsk_l_low_import_IV "Low and Medium Skill Occ.* Log Predicted Low Skill Imports"
	*label var lowsk_Dif_l_low_export_IV_1 "Low and Medium Skill Occ.* Δ Log Predicted Low Skill Exports"
	*label var lowsk_Dif_l_low_import_IV_1 "Low and Medium Skill Occ.* Δ Log Predicted Low Skill Imports"
	
	
	
	global demographics "male age age2 partnered urban"
	
	***** Prepare outcomes:
	global outcomes "trstprl trstlgl trstplt trstprt stfgov stfeco"
	
	foreach var of global outcomes {
		recode `var' (77 88 99 . = .)
	}
	
	* Country and time dummies and trends:
	tab country_gallup, gen(countrydum)
	tab year, gen(yeardum)
	*tab waveyear, gen(yearwave_dum)
	egen country_id = group(country_gallup year)
	egen country_id_wave = group(country_gallup waveyear)
	egen country_id1 = group(country_gallup year tertiary)
	egen country_id2 = group(country_gallup year highsk)
	
	tab country_gallup, nolab
	
	foreach num of numlist 1/8 {
		gen lfstat_`num' = (lfstat == `num')
		replace lfstat_`num' = . if missing(lfstat)
	}
	global employment "lfstat_1-lfstat_8"
	global income "i.income_q"
		
	* Generate new outcomes
	egen average_trust = rmean(trstplt  trstprl  trstprt stfgov)
	pca trstplt  trstprl  trstprt stfgov, comp(1)
	predict fpc_trust_vars, score

	label var highskilled "High Skill Occupation"
	
	cap drop anweight
	gen anweight = pspwght*pweight
	gen sampleR1C2high = (highsk_l_high_export_IV ~= .)
	gen sampleR1C2low = (lowsk_l_low_export_IV ~= .)
		
	egen countryyear = group(country_id year)
	
	
	**#Table 5: OLS and IV Estimates with High-skill and Low-skill Intensive Exports (Imports) and Occupation Interactions - ESS
	foreach var in $outcomes {
		
		gen `var'_dummy = (`var'>= 6) if !missing(`var')
		
	}
	egen average_trust_dummy = rmean(trstplt_dummy  trstprl_dummy  trstprt_dummy stfgov_dummy)
	pca trstplt_dummy  trstprl_dummy  trstprt_dummy stfgov_dummy, comp(1)
	predict fpc_trust_vars_dummy, score
	
	local outcomes average_trust_dummy fpc_trust_vars_dummy
	foreach wgt in anweight {
		
		eststo clear
		
		foreach outcome in `outcomes'{
			
			local Xcovariates2 highsk_l_high_export highsk_l_high_import lowsk_l_low_export lowsk_l_low_import
			local Xordered highsk_l_high_export highsk_l_high_import lowsk_l_low_export lowsk_l_low_import highskilled
			

			eststo: reghdfe `outcome' `Xcovariates2' highskilled $demographics $employment $income [pweight = `wgt'] if sampleR1C2high == 1 & sampleR1C2low == 1, abs(countryyear) cl(countryyear)
			qui estadd local empinc Yes
			qui estadd local cyfe Yes
			qui estadd local dem Yes
			
		}
		
		foreach outcome in `outcomes'{
			
			local Zcovariates2 highsk_l_high_export_IV highsk_l_high_import_IV lowsk_l_low_export_IV lowsk_l_low_import_IV
			
			eststo: ivreghdfe `outcome' (`Xcovariates2' = `Zcovariates2') highskilled $demographics $employment $income [pweight = `wgt'], abs(countryyear) r cluster(countryyear) ffirst
			qui estadd local empinc Yes
			qui estadd local cyfe Yes
			qui estadd local dem Yes
			qui estadd scalar F_KP = round(e(widstat),1)
			
		}

		esttab using "${savet}/Table5.rtf" , keep(`Xordered') order(`Xordered') label mtitle("Average political trust" "First principal component" "IV Average political trust" "IV First principal component") nonumber replace r2(3) b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) nocons noomitted scalars("dem Demographic characteristics" "empinc Employment status and income" "cyfe Country*year fixed effects" "F_KP Kleibergen-Paap rk Wald F Statistic")
	
	}
	

	**# Appendix Table 14:  Gallup Measure is Strongly Correlated with Similar Questions from Other Surveys
	
	use "$conf/Gallup_cleaned.dta", replace

	collapse confidence_government [aw=wgt], by(country_g year)

	save "$conf/Gallup_collapsed.dta", replace

	use "$data/ESS_cleaned.dta", clear

	cap gen anweight = pspwght*pweight

	label var psppsgva "People have say in what government does"
	label var psppipla "People have influence on politics"
	label var trstprl "Trust in parliament"
	label var trstlgl "Trust in legal system"
	label var trstplt "Trust in politicians"
	label var trstprt "Trust in political parties"
	label var trstep "Trust in European Parliament"
	label var stfgov "Satisfaction with national government"
	label var trstun "Trust in UN"

	foreach var in trstplt trstep trstprl trstlgl trstprt stfgov{
		recode `var' (77 88 99 . .b .c .d = .)
	}

	foreach var in psppsgva psppipla{
		recode `var' (7 8 9 . .b .c .d = .)
	}

	collapse trstplt trstep trstprl trstlgl trstprt stfgov psppsgva psppipla [pw=anweight], by(country_g year)


	save "$data/ESS_collapsed.dta", replace

	use "$data/WVS_cleaned.dta", clear

	rename E069_11 Confidence_Government
	rename E069_07 Confidence_Parliament
	rename E069_12 Confidence_Political_parties

	foreach var of varlist Confidence* {
		
		recode `var' (-5 -4 -2 -1 = .)
		
		replace `var' = 4 - `var'
		
	}



	collapse Confidence_Government Confidence_Parliament Confidence_Political_parties [aw = S017], by(country_gallup year)


	save "$data/WVS_collapsed.dta", replace

	use "$conf/Gallup_collapsed.dta", replace

	merge 1:1 country_g year using "$data/ESS_collapsed.dta"

	drop _merge
	merge 1:1 country_g year using "$data/WVS_collapsed.dta"
	
	pwcorr confidence_government trstplt trstprl trstprt stfgov Confidence_Government Confidence_Parliament Confidence_Political_parties, sig
	local rows = rowsof(r(C))
	local cols = colsof(r(C))
	matrix stars = J(1,`rows',0)
	
	forvalues k = 1/`cols' {
		
			matrix stars[1,`k'] = ((abs(r(sig)[`k',1]) <= 0.01) + (abs(r(sig)[`k',1]) <= 0.05) + (abs(r(sig)[`k',1]) <= 0.10)) 
	
	}
	matrix stars = stars[1,2..8]
	mat li r(C)
	matrix C = r(C)[2..8,1]'
	mat li C
	
	frmttable using "$savet/AppendixTable15.rtf",  varlabels statmat(C) sdec(2) substat(0) sq annotate(stars)  asymbol(*,**,***) brackets( "","" \ (,) \ [,]) nobl  replace